package com.itheima.controller;

import com.itheima.common.PageResult7;
import com.itheima.common.Simple;
import com.itheima.entity.SubjectsTags;
import com.itheima.entity.SubjectsTagsDto;
import com.itheima.service.SubjectsTagsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author cfr
 * @date 2022/1/9
 * @TIME:20:40
 */
@RestController
@RequestMapping("/tags")
public class SubjectsTagsController {
    @Autowired
    private SubjectsTagsService subjectsTagsService;
    @Autowired
    private HttpServletRequest request;

    @PostMapping
    public String add(@RequestBody SubjectsTags subjectsTags) {
        //Integer user = Math.toIntExact(((User) request.getSession().getAttribute("user")).getId());
        subjectsTags.setCreatorID(2);
        //subjectsTags.setCreatorID(user);
        return subjectsTagsService.add(subjectsTags);
    }

    @PutMapping("/{id}")
    public String modify(@RequestBody SubjectsTags subjectsTags){
       return subjectsTagsService.modify(subjectsTags);
    }

    //tagName=11&state=1&page=1&pagesize=10
    @GetMapping
    public PageResult7 fingPage(Long page, Long pagesize, String tagName, Integer state,Integer subjectID){
        return subjectsTagsService.finfPage(page,pagesize,tagName,state,subjectID);
    }
    @PostMapping("/{id}/{state}")
    public String state(@PathVariable Integer id,@PathVariable Integer state){
        return subjectsTagsService.state(id,state);
    }
    @DeleteMapping("/{id}")
    public String deleteById(@RequestBody SubjectsTagsDto subjectsTagsDto){
        return subjectsTagsService.delete(subjectsTagsDto);

    }
    @GetMapping("/{id}")
    public SubjectsTagsDto findById(@PathVariable Integer id){
        return subjectsTagsService.fingById(id);
    }

    @GetMapping("/simple")
    public List<Simple> findDirectorys(Integer subjectID) {
        List<Simple> simpless = subjectsTagsService.subjectsTagsService(subjectID);
        return simpless;
    }
}
